
package com.tools.cleanmaster.store;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.manager.GlobalStateMgr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class GlobalConfigSharedPrefManager {

    private static SharedPreferences sPrefs = null;

    private static final String PREFS_KEY_LAST_MEM_TRASH_CLEANNED_TIME = "mem_trash_last_cleanned_time";
    private static final String PREFS_KEY_LAST_MEM_TRASH_CLEANNED_PKGS = "mem_trash_last_cleanned_pkgs";
    private static final String SCAN_LAST_SCAN_TIME = "last_scan_time";
    private static final String LAST_SCAN_NOTIFY_TIME = "last_scan_notify_time";
    private static final String PREFS_KEY_PERSONAL_LANGUAGE = "presonal_language";
    private static final String TRASH_CLEAN_TOTAL_SIZE = "trash_clean_total";
    private static final String TRASH_DEEP_CLEAN = "trash_clean_deep";
    private static final String TRASH_LAST_CLEAN_ALL_DEFAULT_TIME = "trash_clean_all_default";
    private static final String PREFS_KEY_DID = "did";
    private static final String PREFS_KEY_WIZARD_VERSION = "wizard_version";

    private static final String KEY_RESULT_PAGE_ARROW_NUMBER_SHOW = "key_result_page_arrow_number_show";
    private static final String KEY_RESULT_LEFT_OVER_SIZE = "left_over_size";
    private static final String PREFS_KEY_BATTERY_SCALE = "battery_scale";
    private static final String PREFS_KEY_SIGNATURE_TAG = "stag";

    //install info releated
    private static final String PREFS_INSTALL_TIME = "install_time";// 第一次启动时间
    private static final String PREFS_FACEBOOK_DEEPLINK = "facebook_deeplink";// facebook deeplink
    private static final String REPORT_REFERRER = "rr";//安装refer

    public static final String LAST_SHOW_UPDATA_SCENE_TIME = "last_show_updata_scene_time";
    public static final String FIRST_SHOW_UPDATE_NOTIFICATION = "first_show_update_notification";
    public static final String UPDATE_TYPE_SPLASH = "splash";
    public static final String UPDATE_TYPE_NOTIFICATION = "notification";

    private static SharedPreferences initSharedPreferences(Context ctx) {
        if (sPrefs == null) {
            sPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        }
        return sPrefs;
    }

    public static void setLastMemTrashCleanTime(Context context, long time) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putLong(PREFS_KEY_LAST_MEM_TRASH_CLEANNED_TIME, time);
        editor.commit();
    }

    public static long getLastMemTrashCleanTime(Context context) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        return sharedPrefs.getLong(PREFS_KEY_LAST_MEM_TRASH_CLEANNED_TIME, 0);
    }

    public static void setLastMemTrashCleanPkgs(Context context, List<String> pkgs) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        StringBuilder sb = new StringBuilder();
        for (String pkg : pkgs) {
            sb.append(pkg).append(",");
        }
        editor.putString(PREFS_KEY_LAST_MEM_TRASH_CLEANNED_PKGS, sb.toString());
        editor.commit();
    }

    public static List<String> getLastMemTrashCleanPkgs(Context context) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        String str = sharedPrefs.getString(PREFS_KEY_LAST_MEM_TRASH_CLEANNED_PKGS, null);
        if (str != null) {
            String[] strArray = str.split(",");
            if (strArray != null) {
                return new ArrayList<String>(Arrays.asList(strArray));
            }
        }
        return null;
    }

    public static void setScanTime(Context context, long scantime) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putLong(SCAN_LAST_SCAN_TIME, scantime);
        editor.commit();
    }

    public static long getScanTime(Context context) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        return sharedPrefs.getLong(SCAN_LAST_SCAN_TIME, 0);
    }

    public static void setScanNotificationTime(Context context, long time) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putLong(LAST_SCAN_NOTIFY_TIME, time);
        editor.commit();
    }

    public static long getScanNotificationTime(Context context) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        return sharedPrefs.getLong(LAST_SCAN_NOTIFY_TIME, 0);
    }

    public static boolean isPersonalLanguage(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getBoolean(PREFS_KEY_PERSONAL_LANGUAGE, false);
    }

    public static void setTrashCleanTotal(Context ctx, long total) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        SharedPreferencesCompat.apply(prefs.edit().putLong(TRASH_CLEAN_TOTAL_SIZE, total));
    }

    public static long getTrashCleanTotal(Context ctx) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        return prefs.getLong(TRASH_CLEAN_TOTAL_SIZE, 0);
    }

    //随机范围生成booster带量数据
    public static void setDeepClean(Context ctx) {
        SharedPreferences prefs = initSharedPreferences(ctx);
        int max = 70;
        int min = 50;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        SharedPreferencesCompat.apply(prefs.edit().putInt(TRASH_DEEP_CLEAN, s));
    }

    /**
     * 清理掉全部默认勾选项，记录时间
     */
    public static void setCleanAllDefaultTime(Context context, long scantime) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putLong(TRASH_LAST_CLEAN_ALL_DEFAULT_TIME, scantime);
        editor.commit();
    }

    /**
     * 上次清理掉全部默认勾选项时间，用来判断保护时间
     */
    public static long getCleanAllDefaultTime(Context context) {
        SharedPreferences sharedPrefs = initSharedPreferences(context);
        return sharedPrefs.getLong(TRASH_LAST_CLEAN_ALL_DEFAULT_TIME, 0);
    }

    /**
     * If no "did" saved, -1 will be returned.
     */
    public static long getDid(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getLong(PREFS_KEY_DID, -1);
    }

    public static void setDid(Context cxt, long did) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        SharedPreferencesCompat.apply(prefs.edit().putLong(PREFS_KEY_DID, did));
    }

    public static int hasWizardRun(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        int lastWizardVersion = prefs.getInt(PREFS_KEY_WIZARD_VERSION, -1);
        // int curVersionCode = GlobalStateMgr.getInstance().getVersionCode(cxt,0);
        return lastWizardVersion;
    }

    public static void setWizardRun(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        int curVersionCode = GlobalStateMgr.getInstance().getVersionCode(cxt, 0);
        SharedPreferencesCompat.apply(prefs.edit().putInt(PREFS_KEY_WIZARD_VERSION, curVersionCode));
    }

    /**
     * 设置在结果页面中显示箭头动画次数
     */
    public static int getArrowShowNumber(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getInt(KEY_RESULT_PAGE_ARROW_NUMBER_SHOW, 0);
    }

    public static void setArrowShowNumber(Context cxt, int number) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        SharedPreferencesCompat.apply(prefs.edit().putInt(KEY_RESULT_PAGE_ARROW_NUMBER_SHOW, number));
    }

    public static void setLeftOverSize(Context cxt, long leftOverSize) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        SharedPreferencesCompat.apply(prefs.edit().putLong(KEY_RESULT_LEFT_OVER_SIZE, leftOverSize));
    }

    public static long getLeftOverSize(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getLong(KEY_RESULT_LEFT_OVER_SIZE, 0);
    }

    public static int getBatteryScale(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getInt(PREFS_KEY_BATTERY_SCALE, 100);
    }

    public static void setBatteryScale(Context cxt, int scale) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        prefs.edit().putInt(PREFS_KEY_BATTERY_SCALE, scale).apply();
    }

    public static String getSignatureTag(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getString(PREFS_KEY_SIGNATURE_TAG, null);
    }

    public static void saveSignatureTag(Context cxt, String signatureTag) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        prefs.edit().putString(PREFS_KEY_SIGNATURE_TAG, signatureTag)
                .apply();
    }

    public static void setInstallTime(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        if (!prefs.contains(PREFS_INSTALL_TIME)) {
            prefs.edit().putLong(PREFS_INSTALL_TIME, System.currentTimeMillis()).apply();
        }
    }

    public static String getFacebookDeeplink(Context cxt) {
        SharedPreferences prefs = initSharedPreferences(cxt);
        return prefs.getString(PREFS_FACEBOOK_DEEPLINK, "");
    }

    public static String getReportedReferrer(Context cxt) {
        SharedPreferences sharedPrefs = initSharedPreferences(cxt);
        return sharedPrefs.getString(REPORT_REFERRER, "");
    }

    public static void saveReportedReferrer(Context cxt, String reffer) {
        SharedPreferences sharedPrefs = initSharedPreferences(cxt);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString(REPORT_REFERRER, reffer);
        editor.apply();
    }

    public static void setlastShowUpdateSceneTime(String type, long value) {
        initSharedPreferences(CleanApplication.getInstance()).edit().putLong(type + "_"
                + LAST_SHOW_UPDATA_SCENE_TIME, value).apply();
    }

    public static long lastShowUpdateSceneTime(String type) {
        return initSharedPreferences(CleanApplication.getInstance()).getLong(type + "_" + LAST_SHOW_UPDATA_SCENE_TIME, 0);
    }

    public static void setFirstShowUpdateNotification(boolean value) {
        initSharedPreferences(CleanApplication.getInstance()).edit().putBoolean(FIRST_SHOW_UPDATE_NOTIFICATION, value).apply();
    }

    public static boolean firstShowUpdateNotification() {
        return initSharedPreferences(CleanApplication.getInstance()).getBoolean(FIRST_SHOW_UPDATE_NOTIFICATION, true);
    }
}
